package com.wo2record.base.util;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

public final class ModelUtil {

	public static Boolean isBlank(Object obj) {
		if (obj == null)
			return true;
		Class<?> clazz = obj.getClass();
		if (isSimpleType(clazz))
			return "".equals(obj.toString().trim());
		else if (clazz.isArray())
			return Array.getLength(obj) <= 0;
		else if (Collection.class.isAssignableFrom(clazz)) {
			return ((Collection<?>) obj).size() <= 0;
		} else if (Map.class.isAssignableFrom(clazz)) {
			return ((Map<?, ?>) obj).size() <= 0;
		}
		return false;
	}

	public static boolean isSimpleType(Class<?> clazz) {
		return (clazz.isPrimitive() 
				|| String.class.isAssignableFrom(clazz)
				|| Boolean.class.isAssignableFrom(clazz)
				|| Integer.class.isAssignableFrom(clazz)
				|| Long.class.isAssignableFrom(clazz)
				|| Float.class.isAssignableFrom(clazz)
				|| Character.class.isAssignableFrom(clazz)
				|| Short.class.isAssignableFrom(clazz)
				|| Double.class.isAssignableFrom(clazz)
				|| Byte.class.isAssignableFrom(clazz) 
				|| byte[].class.isAssignableFrom(clazz));
	}
}
